package com.power.sifa.supervise.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.plf.common.domain.PageDataResponse;
import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.supervise.dao.Apl_superviseDao;
import com.power.sifa.supervise.model.Apl_supervise;
import com.power.sifa.supervise.model.pageInfo.Apl_supervisePageInfo;
import com.power.sifa.supervise.service.Apl_superviseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 *  复议监督主表业务层实现
 */
@Service
@Slf4j
public class Apl_superviseServiceImpl extends BaseServiceImpl implements Apl_superviseService {


    @Autowired
    private Apl_superviseDao dao;


    @Override
    public Apl_supervise getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Apl_supervise apl_supervise) {
    	// save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (apl_supervise.getPrimaryKey() != null) {
        	 Apl_supervise apl_supervisefDb = dao.findById(apl_supervise.getPrimaryKey()).orElse(null);
             if(apl_supervisefDb != null) {
                BeanUtils.copyProperties(apl_supervise, apl_supervisefDb);
                dao.save(apl_supervisefDb);
                return;
             }
        }
        dao.save(apl_supervise);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Apl_supervise> findAll(Apl_supervisePageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        }else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber()<=1 ? 0 : pageInfo.getPageNumber()-1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }

    @Override
	public List<Apl_supervise> findAll() {
		return dao.findAll();
	}

	@Override
	public void saveAll(List<Apl_supervise> list) {
		dao.saveAll(list);
	}

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFirstAndSecondPerson(List<String> ids, String firstid, String firstname, String secondid, String secondname, String thirdid, String thirdname) {
        if (CollUtil.isNotEmpty(ids)) {
            ids.forEach(id -> this.dao.updateFirstAndSecondPerson(id, firstid, firstname, secondid, secondname, thirdid, thirdname));
        }
    }

    @Override
    public void resetFirstAndSecondPerson(List<String> ids) {
        if (CollUtil.isNotEmpty(ids)) {
            ids.forEach(id -> this.dao.resetFirstAndSecondPerson(id));
        }
    }


    @Override
    public PageDataResponse queryByPage(Apl_supervisePageInfo pageInfo) {
        List<Apl_supervise> list = this.dao.findList(pageInfo);
        // 分页
        PageDataResponse page = new PageDataResponse();
        if (CollectionUtil.isNotEmpty(list)) {
            Long total = Long.valueOf(pageInfo.getAllcount());
            int size = pageInfo.getPageSize() * pageInfo.getPageNumber();
            if (total.intValue() - size > 0) {
                page.setNextPage(pageInfo.getPageNumber() + 1);
            }
            page.setSize(pageInfo.getPageSize());
            page.setCurrent(pageInfo.getPageNumber());
            page.setTotal(total);
        } else {
            page.setSize(0);
            page.setCurrent(1);
            page.setTotal(0L);
        }
        page.setRecords(list);
        return page;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateFinishState(String id, Integer isFinish) {
        this.dao.updateFinishState(id, isFinish);
    }

    @Override
    public  PageDataResponse queryArchivePage(Apl_supervisePageInfo pageInfo) {
        List<Apl_supervise> list = this.dao.queryArchiveData(pageInfo);
        // 分页
        PageDataResponse page = new PageDataResponse();
        if (CollectionUtil.isNotEmpty(list)) {
            for (Apl_supervise data : list) {
                data.geneText();
            }
            Long total = Long.valueOf(pageInfo.getAllcount());
            int size = pageInfo.getPageSize() * pageInfo.getPageNumber();
            if (total.intValue() - size > 0) {
                page.setNextPage(pageInfo.getPageNumber() + 1);
            }
            page.setSize(pageInfo.getPageSize());
            page.setCurrent(pageInfo.getPageNumber());
            page.setTotal(total);
        } else {
            page.setSize(0);
            page.setCurrent(1);
            page.setTotal(0L);
        }
        page.setRecords(list);
        return page;
    }

}
